import Vue from "vue";
import { login, logout, getMenuList } from "@/api/login";
import {
  ACCESS_TOKEN,
  USER_NAME,
  USER_INFO,
  USER_AUTH,
} from "@/store/constant";
import { welcome } from "@/util/util";

export default {
  namespaced: true, //开启模块化
  state: {
    token: "",
    username: "",
    realname: "",
    permissionList: [],
    info: {},
  },
  getters: {
    GET_INFO(state) {
      return Vue.ls.get(USER_INFO);
    },
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_NAME: (state, { username, realname, welcome }) => {
      state.username = username;
      state.realname = realname;
      state.welcome = welcome;
    },
    SET_PERMISSIONLIST: (state, permissionList) => {
      state.permissionList = permissionList;
    },
    SET_INFO: (state, info) => {
      state.info = info;
    },
    SET_ROLEID: (state, roleId) => {
      state.roleId = roleId;
    },
  },
  actions: {
    // 登录
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo)
          .then((response) => {
            if (response.code === 200) {
              const userInfo = response.data;
              const token = response.data.token;

              Vue.ls.set(ACCESS_TOKEN, token, 7 * 24 * 60 * 60 * 1000);
              Vue.ls.set(USER_NAME, userInfo.username, 7 * 24 * 60 * 60 * 1000);
              Vue.ls.set(USER_INFO, userInfo, 7 * 24 * 60 * 60 * 1000);

              commit("SET_TOKEN", token);
              commit("SET_INFO", userInfo);
              commit("SET_NAME", {
                username: userInfo.username,
                realname: userInfo.nickName,
                welcome: welcome(),
              });

              resolve(response);
            } else {
              reject(response);
            }
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    // 登出
    Logout({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout()
          .then(() => {
            commit("SET_TOKEN", "");
            commit("SET_INFO", "");
            commit("SET_NAME", {});
            //清空tabs中的列表
            // commit("tabs/setTabs", [], { root: true });

            Vue.ls.remove(ACCESS_TOKEN);
            Vue.ls.remove(USER_INFO);
            Vue.ls.remove(USER_NAME);
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    //token失效，清空本地缓存
    clearToken({ commit }) {
      commit("SET_TOKEN", "");
      commit("SET_INFO", "");
      commit("SET_NAME", {});
      //清空tabs中的列表
      commit("tabs/setTabs", [], { root: true });

      Vue.ls.remove(ACCESS_TOKEN);
      Vue.ls.remove(USER_INFO);
      Vue.ls.remove(USER_NAME);
    },

    //获取菜单
    getMenuList({ commit }) {
      return new Promise((resolve, reject) => {
        getMenuList()
          .then((response) => {
            let permissionList = response.data;
            commit("SET_PERMISSIONLIST", permissionList);
            resolve(response);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
  },
  modules: {},
};
